package com.helloworld;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.helloworld.bean.CityBean;

import java.util.ArrayList;

public class PopwindowActivity extends AppCompatActivity {

    Button mBtnDown;
    PopupWindow popupWindow;
    private SelectAgeAdapter mSelectAdapter;
    private String jsonCity;
    private CityBean cityBean;
    private ArrayList<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popwindow);
        mBtnDown= (Button) findViewById(R.id.btn_down);
        ListView listView=new ListView(this);
        listView.setDividerHeight(1);
        listView.setCacheColorHint(0x00000000);
        jsonCity=getResources().getString(R.string.city_list);
        cityBean=new Gson().fromJson(jsonCity,CityBean.class);
        mList=new ArrayList<>();
        for (int i=0;i<cityBean.getProvinces().size();i++){
            String provice=cityBean.getProvinces().get(i).getProvinceName();
            mList.add(provice);
        }
        mSelectAdapter = new SelectAgeAdapter();
        listView.setAdapter(mSelectAdapter);
        popupWindow=new PopupWindow(listView,mBtnDown.getWidth(), LinearLayout.LayoutParams.WRAP_CONTENT,true);
        mBtnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()){
                    dismissPopWindow();
                }else {
                    popupWindow.showAsDropDown(mBtnDown);
                }
            }
        });
    }
    public void dismissPopWindow() {
        if(popupWindow != null && popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }

    class SelectAgeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList != null&&mList.size() > 0 ? mList.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(PopwindowActivity.this).inflate(R.layout.item, null);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_name.setText(mList.get(position));
            return convertView;
        }
    }
    class ViewHolder {
        TextView tv_name;
    }
}
